package com.taoist_awei.blog.infrastructure.service;

import com.taoist_awei.blog.core.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.Date;

@Service
public class InitService {
    private UserRepository userRepository;
    private EncryptService encryptService;
    private ArticleRepository articleRepository;
    private IDService idService;
    private ArticleService articleService;
    private ClassifyRepository classifyRepository;
    private String defaultClassifyId;

    @Autowired
    public InitService(UserRepository userRepository,
                       EncryptService encryptService,
                       ArticleRepository articleRepository,
                       DefaultIDService idService,
                       ArticleService articleService,
                       ClassifyRepository classifyRepository,
                       @Value("${default.classify.id}")
                       String defaultClassifyId) {
        this.userRepository = userRepository;
        this.encryptService = encryptService;
        this.articleRepository = articleRepository;
        this.idService = idService;
        this.classifyRepository = classifyRepository;
        ClassifyEntity defaultClassify = new ClassifyEntity();
        defaultClassify.setClassifyName("默认");
        defaultClassify.setFatherId(null);
        defaultClassify.setClassifyId(defaultClassifyId);
        this.classifyRepository.addClassify(defaultClassify);
        String id = idService.getID();
        String content = "# 这是我的第一篇博客\n这个网站使用Springboot+Mybatis+Spring Security+Vue-cli等技术搭建而成。";
        Integer readTime = articleService.getReadTime(content);
        String summary = articleService.getSummary(content);
        Integer words = articleService.getWords(content);
        ArticleEntity articleEntity = new ArticleEntity (
                "第一篇博客",
                new Date(),
                readTime,
                "原创",
                content,
                summary,
                defaultClassifyId,
                id,
                new Date()
        );
        articleEntity.publish();
        articleEntity.setWords(words);
        articleEntity.setClassifyName("默认");
        UserEntity userEntity = new UserEntity("阿威道长", "gaowei20140719", "2592006268@qq.com");
        userEntity.setLocation("成都 中国");
        userEntity.setIntroduce("技术只是工具，想法才是最重要的");
        String encrypt = this.encryptService.encrypt(userEntity.getPassword());
        userEntity.setPassword(encrypt);
        this.userRepository.save(userEntity);
        this.articleRepository.saveArticle(articleEntity);
        this.articleRepository.publish(this.defaultClassifyId);
    }
}
